package com.easy.common.util;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

public class MDate {

	public static final int WITHGROUP = 0;
	public static final int WITHOUTGROUP = 1;
	public static String DATE_PATTERN = "yyyy-MM-dd";
	public static String DATE_PATTEN_SECOND = "yyyy-MM-dd HH:mm:ss";
	public static long DATE_DAY = 86400000L;
	public static long DATE_HOUR = 3600000L;
	public static long DATE_MINIT = 60000L;

//	private static DecimalFormat numFormat = new DecimalFormat("#0.00");
//	private static DecimalFormat numFormatG = new DecimalFormat("#,##0.00");
	private static SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat dateFormatS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	public static String DATE_FORMAT_NOSPLIT = "yyyyMMddHHmmssSSS";
	private static SimpleDateFormat dateFormatEn;
	private static SimpleDateFormat dateFormatCn;
	private static SimpleDateFormat dateFormatYMEn;
	private static SimpleDateFormat dateFormatYMCn;
	private static SimpleDateFormat dateFormatMDEn;
	private static SimpleDateFormat dateFormatMDCn;
	private static SimpleDateFormat dateFormatMDHMCn;
	private static SimpleDateFormat dateFormatYMDHMCn;
	public static final int decimalScale = 2;
//	private static final String enSmallNumber[] = {
//		"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", 
//		"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"
//	};
//	private static final String enLargeNumber[] = {
//		"TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"
//	};
//	private static final String enUnit[] = {
//		"", "THOUSAND", "MILLION", "BILLION", "TRILLION"
//	};
//	private static final Class MYUtility = null;
	public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

	public MDate()
	{
	}
	/**
	 * 取的当前时间的字符串．
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurDateTimeStr()
	{
		return dateFormat.format(new Date());
	}

	/**
	 * 取的当前日期的字符串．
	 * @return yyyy-MM-dd
	 */
	public static String getCurDateStr()
	{
		return dFormat.format(new Date());
	}

	/**
	 * 按指定格式　取的当前时间的字符串
	 * @param pattern yyyy-MM-dd HH:mm:ss
	 * @return 按pattern的格式返回
	 */
	public static String getCurDateTimeStr(String pattern)
	{
		if (pattern == null)
		{
			return getCurDateTimeStr();
		} else
		{
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			return format.format(new Date());
		}
	}

	/**
	 * 按指定格式　取的当前时间的字符串
	 * @param format 格式见java.text.SimpleDateFormat
	 * @return
	 */
	public static String getCurDateTimeStr(SimpleDateFormat format)
	{
		if (format == null)
			return getCurDateTimeStr();
		else
			return format.format(new Date());
	}

	/**
	 * 将日期date转换为指定格式的字符串．
	 * @param date 日期时间变量
	 * @param pattern 格式 yyyy-MM-dd HH:mm:ss
	 * @return　pattern指定的格式日期字符串
	 */
	public static String getDateTimeStr(Date date,String pattern)
	{
		if(pattern==null){
			if (date == null)
				return getCurDateTimeStr();
			else
				return dateFormat.format(date);
		}
		else {
			if (date == null)
				return getCurDateTimeStr(pattern);
			else {
				SimpleDateFormat format = new SimpleDateFormat(pattern);
				return format.format(date);
			}
		}
		
	}
	
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　0 1 2 3 4 5 6 星期日 星期一 星期二　星期三　星期四　星期五　星期六 
	 */
	public static int getDateWeek(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		if(date != null)calendar.setTime(date);      
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return w;
	}
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　星期日  星期一 星期二　星期三　星期四　星期五　星期六
	 */
	public static String getDateWeekStr(Date date)
	{
		String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; 
		Calendar calendar = Calendar.getInstance();
		if(date != null)calendar.setTime(date);      
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return weekOfDays[w];
	}
	
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　0 1 2 3 4 5 6 星期日 星期一 星期二　星期三　星期四　星期五　星期六 
	 */
	public static int getCurWeek()
	{
		Calendar calendar = Calendar.getInstance();
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return w;
	}
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　星期日  星期一 星期二　星期三　星期四　星期五　星期六
	 */
	public static String getCurWeekStr()
	{
		String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; 
		Calendar calendar = Calendar.getInstance();
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return weekOfDays[w];
	}

	
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　0 1 2 3 4 5 6 星期日 星期一 星期二　星期三　星期四　星期五　星期六 
	 */
	public static int getWeek(Date tdate)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(tdate);
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return w;
	}
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　星期日  星期一 星期二　星期三　星期四　星期五　星期六
	 */
	public static String getWeekStr(Date tdate)
	{
		String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; 
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(tdate);
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return weekOfDays[w];
	}
	
	/**
	 *将BigDecimal转化为金额字符串 
	 * @param num BigDecimal类型变量
	 * @return 金额格式字符串
	 */
	public static String getAmountStr(BigDecimal num){
		if(num==null)return "0.00";
		else {
			num=num.setScale(2,BigDecimal.ROUND_HALF_UP);
			return num.toString();
		}
	}

	/**
	 * 将日期date转换为 yyyy-MM-dd HH:mm:ss 格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy-MM-dd HH:mm:ss格式的日期字符串
	 */
	public static String getDateTimeStr(Date date)
	{
		if (date == null)
			return getCurDateTimeStr();
		else
			return dateFormat.format(date);
	}
	/**
	 * 
	 * @param adate
	 * @param ddate
	 */
	@SuppressWarnings("deprecation")
	public static int getMonthDiff(Date adate,Date ddate)
	{
		int mon=(ddate.getYear()-adate.getYear())*12+ddate.getMonth()-adate.getMonth();
		return mon;
	}
	
	/**
	 * 将日期date转换为 yyyy-MM-dd格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy-MM-dd格式的日期字符串
	 */

	public static String getStr(Date date,String fmt)
	{
		if (date == null)
			return getCurStr(fmt);
		else{
	        SimpleDateFormat formatter = new SimpleDateFormat(fmt);
	        String strDate = formatter.format(date);		
			return strDate;
		}
	}
	/**
	 * 将日期date转换为 yyyy-MM-dd格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy-MM-dd格式的日期字符串
	 */

	public static String getCurStr(String fmt)
	{
        SimpleDateFormat formatter = new SimpleDateFormat(fmt);
        Date t=new Date();
        String strDate = formatter.format(t);		
		return strDate;
	}
	
	/**
	 * 将日期date转换为 yyyy-MM-dd格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy-MM-dd格式的日期字符串
	 */

	public static String getDateStr(Date date)
	{
		if (date == null)
			return getCurDateStr();
		else
			return dFormat.format(date);
	}

	/**
	 * 将日期date转换为 dd-MM-yyyy 英文格式的字符串
	 * @param date 日期时间变量
	 * @return dd-MM-yyyy格式的日期字符串
	 */
	public static String getDateTimeStrEn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatEn);
		else
			return dateFormatEn.format(date);
	}

	/**
	 * 将日期date转换为 yyyy年MM月dd日中文格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy年MM月dd日 格式的日期字符串
	 */
	public static String getDateTimeStrCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatCn);
		else
			return dateFormatCn.format(date);
	}

	/**
	 * 将日期字符串dateStr转换为 dd-MM-yyyy 英文格式的字符串
	 * @param date 日期时间变量
	 * @return dd-MM-yyyy格式的日期字符串
	 */
	public static String getDateTimeStrEn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatEn);
		else
			return getDateTimeStrEn(parseDate(dateStr));
	}

	/**
	 * 将日期字符串dateStr转换为 yyyy年MM月dd日中文格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy年MM月dd日中文 格式的日期字符串
	 */
	public static String getDateTimeStrCn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatCn);
		else
			return getDateTimeStrCn(parseDate(dateStr));
	}

	/**
	 * 将日期date转换为 MM-yyyy 英文格式的字符串
	 * @param date 日期时间变量
	 * @return dd-MM-yyyy格式的日期字符串
	 */
	public static String getDateTimeStrYMEn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatYMEn);
		else
			return dateFormatYMEn.format(date);
	}

	public static String getDateTimeStrYMCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatYMCn);
		else
			return dateFormatYMCn.format(date);
	}

	public static String getDateTimeStrYMEn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatYMEn);
		else
			return getDateTimeStrYMEn(parseDate(dateStr));
	}

	public static String getDateTimeStrYMCn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatYMCn);
		else
			return getDateTimeStrYMCn(parseDate(dateStr));
	}

	public static String getDateTimeStrMDEn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatMDEn);
		else
			return dateFormatMDEn.format(date);
	}

	public static String getDateTimeStrMDCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatMDCn);
		else
			return dateFormatMDCn.format(date);
	}
	
	public static String getDateTimeStrYMDHMCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatYMDHMCn);
		else
			return dateFormatYMDHMCn.format(date);
	}

	public static String getDateTimeStrMDHMCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatMDHMCn);
		else
			return dateFormatMDHMCn.format(date);
	}

	public static String getDateTimeStrMDEn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatMDEn);
		else
			return getDateTimeStrMDEn(parseDate(dateStr));
	}

	public static String getDateTimeStrMDCn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatMDCn);
		else
			return getDateTimeStrMDCn(parseDate(dateStr));
	}

	private static Calendar clearDate(Date date, int clearNum)
	{
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		if (clearNum > 0)
			cal.set(14, 0);
		if (clearNum > 1)
			cal.set(13, 0);
		if (clearNum > 2)
			cal.set(12, 0);
		if (clearNum > 3)
			cal.set(11, 0);
		if (clearNum > 4)
			cal.set(5, 0);
		if (clearNum > 5)
			cal.set(2, 0);
		return cal;
	}

	public static String[] getDateLimitStr()
		throws Exception
	{
		return getDateLimitStr(new Date());
	}

	public static String[] getWeekLimitStr()
		throws Exception
	{
		return getWeekLimitStr(new Date());
	}

	public static String[] getMonthLimitStr()
		throws Exception
	{
		return getMonthLimitStr(new Date());
	}

	public static String[] getYearLimitStr()
		throws Exception
	{
		return getYearLimitStr(new Date());
	}

	public static String[] getDateLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getDateLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static String[] getWeekLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getWeekLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static String[] getMonthLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getMonthLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static String[] getYearLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getYearLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static Date[] getDateLimit(Date date)
		throws Exception
	{
		Calendar cal = clearDate(date, 4);
		Date date1 = cal.getTime();
		cal.add(5, 1);
		cal.add(13, -1);
		Date date2 = cal.getTime();
		return (new Date[] {
			date1, date2
		});
	}

	public static Date getFirstDayOfWeek(Date date)
	{
		Calendar c = clearDate(date, 4);
		c.setFirstDayOfWeek(2);
		c.set(7, c.getFirstDayOfWeek());
		return c.getTime();
	}

	public static Date getLastDayOfWeek(Date date)
	{
		Calendar c = clearDate(date, 4);
		c.setFirstDayOfWeek(2);
		c.set(7, c.getFirstDayOfWeek() + 6);
		c.set(11, 23);
		c.set(12, 59);
		c.set(13, 59);
		c.set(14, 0);
		return c.getTime();
	}

	public static Date[] getWeekLimit(Date date)
		throws Exception
	{
		Date date1 = getFirstDayOfWeek(date);
		Date date2 = getLastDayOfWeek(date);
		return (new Date[] {
			date1, date2
		});
	}

	public static Date[] getMonthLimit(Date date)
		throws Exception
	{
		Calendar cal = clearDate(date, 5);
		cal.set(5, 1);
		Date date1 = cal.getTime();
		cal.add(2, 1);
		cal.add(13, -1);
		Date date2 = cal.getTime();
		return (new Date[] {
			date1, date2
		});
	}

	public static Date[] getYearLimit(Date date)
		throws Exception
	{
		Calendar cal = clearDate(date, 6);
		cal.set(2, 0);
		cal.set(5, 1);
		Date date1 = cal.getTime();
		cal.add(1, 1);
		cal.add(13, -1);
		Date date2 = cal.getTime();
		return (new Date[] {
			date1, date2
		});
	}

	public static int getDaySpan(String toDateStr)
		throws Exception
	{
		return getDaySpan(new Date(),parseDate(toDateStr));
	}

	public static int getDaySpan(Date toDate)
	{
		return getDaySpan(new Date(),toDate);
	}

	public static int getDaySpan(Date fromDate, Date toDate)
	{
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			fromDate=sdf.parse(sdf.format(fromDate));
			toDate=sdf.parse(sdf.format(toDate));
			Calendar cal = Calendar.getInstance();    
			cal.setTime(fromDate);
			long time1 = cal.getTimeInMillis();                 
			cal.setTime(toDate);
			long time2 = cal.getTimeInMillis();         
			long between_days=(time2-time1)/(1000*3600*24);  
	        
			return Integer.parseInt(String.valueOf(between_days)); 		
		} catch (ParseException e) {
			return 0;
		}  
	}

	
	public static int getTimeSpan(String toDateStr)
	throws Exception
	{
		return (int)((parseDate(toDateStr).getTime() - (new Date()).getTime()) / 1000);
	}

	public static int getTimeSpan(Date toDate)
	{
		return (int)((toDate.getTime() - (new Date()).getTime()) / 1000);
	}

	public static int getTimeSpan(Date fromDate, Date toDate)
	{
		return (int)((toDate.getTime() - fromDate.getTime()) / 1000);
	}
	
	public static Date getDateBefore()
			throws Exception
	{
		return getDateAdd(new Date(), -1);
	}

	public static Date getDateAdd(String dateStr, int dayCnt)
		throws Exception
	{
		return getDateAdd(parseDate(dateStr), dayCnt);
	}

	public static Date getDateAdd(Date date, int dayCnt)
	{
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, dayCnt);
		return cal.getTime();
	}
	public static Date getDateAfter()
			throws Exception
	{
		return getDateAdd(new Date(), 1);
	}
	
	public static Date getDayBefore()
			throws Exception
	{
		return getDayBefore(new Date(), 1);
	}

	public static Date getDayBefore(String dateStr, int dayCnt)
		throws Exception
	{
		return getDayBefore(parseDate(dateStr), dayCnt);
	}

	public static Date getDayBefore(Date date, int dayCnt)
	{
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, 0 - dayCnt);
		return cal.getTime();
	}

	public static Date getDayAfter(String dateStr, int dayCnt)
		throws Exception
	{
		return getDayAfter(parseDate(dateStr), dayCnt);
	}

	public static Date getDayAfter()
			throws Exception
	{
		return getDayAfter(new Date(), 1);
	}

	public static Date getDayAfter(Date date, int dayCnt)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, dayCnt);
		return cal.getTime();
	}

	public static Date getDayDiff(String dateStr, int dayCnt)
		throws Exception
	{
		return getDayDiff(parseDate(dateStr), dayCnt);
	}

	public static Date getDayDiff(Date date, int dayCnt)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, dayCnt);
		return cal.getTime();
	}

	public static Date getYestday(String dateStr)
		throws Exception
	{
		return getYestday(parseDate(dateStr));
	}

	public static Date getYestday(Date date)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, -1);
		return cal.getTime();
	}

	public static String getYestdayStr(String dateStr)
		throws Exception
	{
		return getYestdayStr(parseDate(dateStr));
	}

	public static String getYestdayStr(Date date)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, -1);
		return getDateTimeStr(cal.getTime());
	}

	public static String getMonthBefore(String dateStr, int diff)
		throws Exception
	{
		return getMonthBefore(parseDate(dateStr), diff);
	}

	public static String getMonthBefore(Date date, int diff)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(2, 0 - diff);
		return getDateTimeStr(cal.getTime());
	}
	public static Date getMonthBefore1(String dateStr, int diff)
			throws Exception
	{
		return getMonthBefore1(parseDate(dateStr), diff);
	}

	public static Date getMonthBefore1(Date date, int diff)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(2, 0 - diff);
		return cal.getTime();
	}

	
	public static String getMonthAfter(String dateStr, int diff)
		throws Exception
	{
		return getMonthAfter(parseDate(dateStr), diff);
	}

	public static Date getMonthAfter1(String dateStr, int diff)
			throws Exception
	{
		return getMonthAfter1(parseDate(dateStr), diff);
	}

	public static String getMonthAfter(Date date, int diff)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(2, 0 + diff);
		return getDateTimeStr(cal.getTime());
	}

	public static Date getMonthAfter1(Date date, int diff)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(2, 0 + diff);
		return cal.getTime();
	}

	public static boolean isAcctYMFormat(String str)
	{
		if (str == null || str.equals(""))
			return false;
		else
			return Pattern.matches("\\d{4}-\\d{2}", str);
	}

	public static boolean isSysDateFormat(String str)
	{
		if (str == null || str.equals(""))
			return false;
		if (str.length() < 10)
			return false;
		if (str.length() == 10)
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}", str);
		else
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}", str);
	}

	public static boolean isDay(String str)
	{
		if (str == null || str.equals(""))
			return false;
		if (str.length() < 10)
			return false;
		else
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}", str);
	}

	public static boolean isDayTime(String str)
	{
		if (str == null || str.equals(""))
			return false;
		if (str.length() < 19)
			return false;
		else
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}", str);
	}

	public static boolean isBracketMatched(String expr)
	{
		if (expr == null || expr.equals(""))
			return true;
		int tmpIndex = expr.indexOf('(');
		if (tmpIndex < 0)
			return true;
		int stackInt = 1;
		for (tmpIndex++; tmpIndex < expr.length();)
		{
			char c = expr.charAt(tmpIndex++);
			if (c == '(')
				stackInt++;
			else
			if (c == ')')
				stackInt--;
		}

		return stackInt == 0;
	}
	/**
	 * 将日期字符串转化为日期Date型
	 * @param formatStr 转化格式 yyyy-MM-dd,yyyy年MM月dd日,MMMyyyy等
	 * @param dateStr 日期字符串
	 * @return 返回日期date型
	 * @throws ParseException
	 */
	public static Date parseDate(String formatStr, String dateStr)
		throws ParseException
	{
		if(dateStr==null || dateStr.length()<1)return new Date(0);
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.parse(dateStr);
	}

	public static Date parseDate(String dateStr)
		throws ParseException
	{
		if (dateStr == null || "".equals(dateStr))
			return null;
		SimpleDateFormat format = null;
		if (Pattern.matches("\\d{4}-\\d{1,2}-\\d{1,2}", dateStr))
			format = new SimpleDateFormat("yyyy-MM-dd");
		else
		if (Pattern.matches("\\d{4}\\d{2}\\d{2}", dateStr))
			format = new SimpleDateFormat("yyyyMMdd");
		else
		if (Pattern.matches("\\d{4}年\\d{2}月\\d{2}日", dateStr))
			format = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
		else
		if (Pattern.matches("\\d{4}年\\d{1,2}月\\d{1,2}日", dateStr))
			format = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA);
		else
		if (Pattern.matches("\\d{1,2}\\w{3}\\d{4}", dateStr))
			format = new SimpleDateFormat("dMMMyyyy", Locale.ENGLISH);
		else
		if (Pattern.matches("\\d{1,2}-\\w{3}-\\d{4}", dateStr))
			format = new SimpleDateFormat("d-MMM-yyyy", Locale.ENGLISH);
		else
		if (dateStr.length() > 20)
			format = dateFormatS;
		else
			format = dateFormat;
		return format.parse(dateStr);
	}
	@SuppressWarnings("deprecation")
	public static Date toDate(String dateStr)
	throws ParseException
	{
		if (dateStr == null || "".equals(dateStr) || dateStr.indexOf("0000-00-00")>=0)
			return null;
		int y=0,m=0,d=0,h=0,n=0,s=0;
    	int pos;

    	pos=dateStr.indexOf("-");
    	if(pos<0){
    		pos=dateStr.indexOf(":");
    		if(pos<0)return null;
    		
    		h=Integer.parseInt(dateStr.substring(0,pos));
	    	dateStr=dateStr.substring(pos+1);
    		pos=dateStr.indexOf(":");
    		if(pos<0){
    			n=Integer.parseInt(dateStr);
    			s=0;
    		}
    		else{
    			n=Integer.parseInt(dateStr.substring(0,pos));
    			dateStr=dateStr.substring(pos+1);
    			pos=dateStr.indexOf(".");
    			if(pos<0)s=Integer.parseInt(dateStr);
    			else s=Integer.parseInt(dateStr.substring(0,pos));
    		}
    	}
    	else {
	    	y=Integer.parseInt(dateStr.substring(0,pos));
	
	    	dateStr=dateStr.substring(pos+1);
	    	pos=dateStr.indexOf("-");
	    	m=Integer.parseInt(dateStr.substring(0,pos));
	
	    	dateStr=dateStr.substring(pos+1);
	    	pos=dateStr.indexOf(" ");
	    	if(pos<0){
	    		d=Integer.parseInt(dateStr);
	    		h=0;
	    		n=0;
	    		s=0;
	    	}
	    	else{
	    		d=Integer.parseInt(dateStr.substring(0,pos));
		    	dateStr=dateStr.substring(pos+1);
	    		pos=dateStr.indexOf(":");
	    		h=Integer.parseInt(dateStr.substring(0,pos));
		    	dateStr=dateStr.substring(pos+1);
	    		pos=dateStr.indexOf(":");
	    		if(pos<0){
	    			n=Integer.parseInt(dateStr);
	    			s=0;
	    		}
	    		else{
	    			n=Integer.parseInt(dateStr.substring(0,pos));
	    			dateStr=dateStr.substring(pos+1);
	    			pos=dateStr.indexOf(".");
	    			if(pos<0)s=Integer.parseInt(dateStr);
	    			else s=Integer.parseInt(dateStr.substring(0,pos));
	    		}
	    	}
    	}
    	return new Date(y-1900,m-1,d,h,n,s);
	}

	public static Timestamp parseTimeStamp()
		throws ParseException
	{
		return parseTimeStamp(((String) (null)));
	}

	public static Timestamp parseTimeStamp(long longDate)
		throws ParseException
	{
		Date utilDate = new Date(longDate);
		Timestamp sqlDate = new Timestamp(utilDate.getTime());
		return sqlDate;
	}

	public static Timestamp parseTimeStamp(String dateStr)
		throws ParseException
	{
		Date utilDate = null;
		if (dateStr == null)
			utilDate = new Date();
		else
			utilDate = parseDate(dateStr);
		Timestamp sqlDate = new Timestamp(utilDate.getTime());
		return sqlDate;
	}

	public static java.sql.Date parseSqlDate()
		throws ParseException
	{
		return parseSqlDate("");
	}

	public static java.sql.Date parseSqlDate(String dateStr)
		throws ParseException
	{
		Date utilDate = null;
		if (dateStr == null || dateStr.length()<1)
			utilDate = new Date();
		else
			utilDate = parseDate(dateStr);
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	}
	public static int compareToDate(Date fdate, Date tdate){
		try{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			fdate=sdf.parse(sdf.format(fdate));
			tdate=sdf.parse(sdf.format(tdate));
		}catch(Exception ex){}
		return fdate.compareTo(tdate);
	}
	

	public static java.sql.Date parseSqlDate(Date date)
	throws ParseException
	{
		if (date == null)return null;
		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}

	static 
	{
		dateFormatEn = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		dateFormatCn = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINESE);
		dateFormatYMEn = new SimpleDateFormat("MMM-yyyy", Locale.ENGLISH);
		dateFormatYMCn = new SimpleDateFormat("yyyy年MM月", Locale.CHINESE);
		dateFormatMDEn = new SimpleDateFormat("dd-MMM", Locale.ENGLISH);
		dateFormatMDCn = new SimpleDateFormat("MM-dd", Locale.CHINESE);
		dateFormatMDHMCn = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINESE);
		dateFormatYMDHMCn = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINESE);
	}

	public static String getTimeStr(Time time) {
		return null;
	}
}
